package vn.orderonline.web.admin.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.orderonline.com.core.business.member.IMember;
import vn.orderonline.com.core.business.member.IReside;
import vn.orderonline.core.redis.RedisMember;

@Controller
@RequestMapping("/member")
public class MemberController {
	
	private static final Logger LOGGER = Logger
			.getLogger(MemberController.class);
	
	@Autowired
	private IMember iMember;
	@Autowired
	private IReside iReside;

	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
	public String listMember(ModelMap model) {
		LOGGER.info("START TO GET LIST MEMBER!");
		try {
			model.addAttribute("member", new RedisMember());
			model.addAttribute("list", iMember.getAllMember());
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO GET LIST MEMBER!");
		return "/member/search-member";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	public String search(ModelMap model, @ModelAttribute RedisMember member) {
		LOGGER.info("START SEARCH MEMBER!");
		try {
			model.addAttribute("list", iMember.search(member));
			model.addAttribute("member", member);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END SEARCH MEMBER!");
		return "/member/search-member";
	}
	
	@RequestMapping(value = "/detail", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
	public String detail(ModelMap model, @RequestParam String username) {
		LOGGER.info("START TO VIEW DETAIL MEMBER!");
		try {
			model.addAttribute("member", iMember.getMemberByUsername(username));
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO VIEW DETAIL MEMBER!");
		return "/member/detail-member";
	}
	
	@RequestMapping(value = "/form", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
	public String formAdd(ModelMap model) {
		LOGGER.info("START CREATE FROM ADD NEW MEMBER!");
		try {
			model.addAttribute("member", new RedisMember());
			model.addAttribute("map", iReside.getAllReside());
		} catch (Exception e) {
			LOGGER.info(e.getMessage(), e);
		}
		LOGGER.info("END CREATE FORM ADD NEW MEMBER!");
		return "/member/add-member";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	public String add(@ModelAttribute RedisMember member, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START TO ADD NEW MEMBER!");
			member.setCreateId(request.getUserPrincipal().getName());
			try {
				iMember.addMember(member);
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}
			LOGGER.info("END TO ADD NEW MEMBER!");
		}
		return request.getUserPrincipal() != null ? "redirect:/member/list" : "redirect:/adminaccount/login";
	}
	
	@RequestMapping(value = "/checkexistedusername", method = RequestMethod.POST)
	@ResponseBody public String checkExistedUsername(@RequestParam String username) {
		LOGGER.info("CHECK EXISTED USERNAME!");
		Boolean check = false;
		try {
			check = iMember.checkExistedUsername(username);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END CHECD EXISTED USERNAME!");
		return check.toString();
	}
	
	@RequestMapping(value = "/checkexistedemail", method = RequestMethod.POST)
	@ResponseBody public String checkExistedEmail(@RequestParam String email) {
		LOGGER.info("CHECK EXISTED EMAIL!");
		Boolean check = false;
		try {
			check = iMember.checkExistedEmail(email);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END CHECD EXISTED USERNAME!");
		return check.toString();
	}
	
	@RequestMapping(value = "/updateform", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String formUpdate(ModelMap model, @RequestParam String username) {
		LOGGER.info("START CREATE FORM UPDATE!");
		try {
			model.addAttribute("member", iMember.getMemberByUsername(username));
			model.addAttribute("map", iReside.getAllReside());
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END CREATE FORM UPDATE!");
		return "/member/update-member";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String update(@ModelAttribute RedisMember member, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START UPDATE MEMBER!");
			member.setUpdateId(request.getUserPrincipal().getName());
			try {
			 	iMember.update(member);
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}
			LOGGER.info("END UPDATE MEMBER!");
			return "redirect:/member/list";
		}
		return "redirect:/adminaccount/login";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String delete(@RequestParam String username) {
		LOGGER.info("START DELETE MEMBER!");
		try {
			iMember.delete(username);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END DELETE MEMBER!");
		return "redirect:/member/list";
	}
}
